##=============================================================================
## Appendix: Table 9
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "lme4", "sjPlot", "ordinal", "texreg", "hrbrthemes",
              "sandwich", "lmtest")


ipak(packages)

##---------
# Load Data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("~/Desktop/clean_ukraine_data.RData")

##--------------------------------------
# Vanilla OLS with SEs clustered at ADM2
coeftest(lm(save_lives_z ~ survival_mindset_z + per_idx_invcov + fam_idx_invcov +
              viina_cum_adm2_z + frontline_b + displaced + sex + rus_ethnic +
              age + edu + job + finances, dat), 
         vcov = vcovCL, 
         type = "HC1", 
         cluster = ~adm2_en)

coeftest(lm(compromise_idx_terr_z ~ survival_mindset_z + per_idx_invcov + 
              fam_idx_invcov + viina_cum_adm2_z + frontline_b + displaced + 
              sex + rus_ethnic + age + edu + job + finances, dat), 
         vcov = vcovCL, 
         type = "HC1", 
         cluster = ~adm2_en)

##---------------------
# Two-Way Fixed Effects
labs <- c("Survival Mindset (z-std)",
          "Personal Victimization",
          "Family Victimization",
          "VIINA (ADM2)",
          "Frontline Oblast",
          "Displaced",
          "Sex",
          "Ethnic Russian",
          "Age",
          "Education",
          "Employment",
          "Socio-Economic Status")

## Save Lives
fit1 <- plm(save_lives_z ~ survival_mindset_z + per_idx_invcov + fam_idx_invcov +
              viina_cum_adm2_z + frontline_b + displaced + sex + rus_ethnic +
              age + edu + job + finances,
            data = dat, 
            index = c("wave", "adm2_pcode"), 
            model = "within", 
            effect = "twoways")

coeftest(fit1, vcov = vcovHC, type = "HC1")

coefplot::coefplot(fit1, intercept = FALSE, sort = "magnitude") +  
  scale_color_grey() +
  labs(x = "Coefficient Value (z-std)", y = "",
       title = "Two-Way Fixed Effects Model: Save Lives (z-std)") +
  #  scale_y_discrete(labels = labs) +
  theme_bw(base_size = 20) + 
  theme(legend.position = "none") 

## Compromise
fit2 <- plm(compromise_idx_terr_z ~ survival_mindset_z + per_idx_invcov + 
              fam_idx_invcov + viina_cum_adm2_z + frontline_b + displaced + 
              sex + rus_ethnic + age + edu + job + finances,
            data = dat, 
            index = c("wave", "adm2_pcode"), 
            model = "within", 
            effect = "twoways") 

coeftest(fit2, vcov = vcovHC, type = "HC1")

coefplot::coefplot(fit2, intercept = FALSE, sort = "magnitude") +  
  scale_color_grey() +
  labs(x = "Coefficient Value (z-std)", y = "",
       title = "Two-Way Fixed Effects Model: Compromise (z-std)") +
  # scale_y_discrete(labels = labs) +
  theme_bw(base_size = 20) + 
  theme(legend.position = "none") 

##------------------------
# Export Table to Appendix
texreg(list(fit1, fit2), 
       dcolumn = TRUE,
       custom.coef.names = labs,
       custom.model.names = c("Save Lives (ordinal)", "Compromise (contin."))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================